package com.controller;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import com.entity.Music;
import com.entity.Special;
import com.entity.Data.PagerData;
import com.entity.Query.MusicQuery;
import com.service.MusicListService;
import com.service.MusicService;
import com.service.MusicTypeService;
import com.service.SingerService;
import com.service.SpecialService;

@RequestMapping("/music")
@Controller
public class MusicController {
	
	@Autowired
	private MusicService musicService;
	@Autowired
	private MusicTypeService musicTypeService;
	@Autowired
	private SpecialService specialService;
	@Autowired
	private MusicListService musicListService;
	@Autowired
	private SingerService singerService;
	
	@RequestMapping("music_show")
	public String music_show(Model model,Integer curPage,MusicQuery musicQuery) {
		//如果没有页数，则页数默认为1
		if(curPage==null){
			curPage = 1;
		}
		
		PagerData musicList = musicService.queryByPage(musicQuery, curPage, 5);
		model.addAttribute("musicList", musicList);
		
		List<Map<String, Object>> musicTypeList = musicTypeService.queryMusicType();
		model.addAttribute("musicTypeList", musicTypeList);
		
		List<Map<String, Object>> specialList = specialService.querySpecial();
		model.addAttribute("specialList", specialList);
		
		List<Map<String, Object>> musicListList = musicListService.queryMusicList();
		model.addAttribute("musicListList", musicListList);
		
		List<Map<String, Object>> singerList = singerService.querySinger();
		model.addAttribute("singerList", singerList);
		return "/admin/music";
	}
	
	@RequestMapping("music_add_show")
	public String music_add_show(Model model) {
		List<Map<String, Object>> musicTypeList = musicTypeService.queryMusicType();
		List<Map<String, Object>> specialList = specialService.querySpecial();
		List<Map<String, Object>> musicListList = musicListService.queryMusicList();
		List<Map<String, Object>> singerList = singerService.querySinger();
		model.addAttribute("musicTypeList", musicTypeList);
		model.addAttribute("specialList", specialList);
		model.addAttribute("musicListList", musicListList);
		model.addAttribute("singerList", singerList);
		return "/admin/music-add";
	}
	
	@RequestMapping("music_add_submit")
	public String music_add_submit(@RequestParam(required=false) MultipartFile[] wj,HttpServletRequest request) {
		Music music = new Music();
		//获取保存文件的服务器目录
		String saveFilePath = request.getServletContext().getRealPath("myfile/music/");
		//遍历每个上传文件进行保存
		for(MultipartFile mf : wj){
			//文件名
			String oldFileName = mf.getOriginalFilename();
			//扩展名
			String ext = FilenameUtils.getExtension(oldFileName);
			System.out.println(oldFileName+" "+ext);
			//新的文件名
			String newFileName = UUID.randomUUID()+"_"+oldFileName;
			//构建要保存的文件对象
			File f=new File(saveFilePath,newFileName);
			if(!f.exists()){
				f.mkdirs();
			}
			//将上传文件存到指定的文件对象上
			try {
				mf.transferTo(f);
				if("m4a".equals(ext)){
					music.setMusicMp3("myfile/music/"+newFileName);
				}else if("mp3".equals(ext)){
					music.setMusicMp3("myfile/music/"+newFileName);
				}else if("jpg".equals(ext) || "png".equals(ext)){
					music.setMusicPhoto("myfile/music/"+newFileName);
				}
			} catch (IllegalStateException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		String musicName = request.getParameter("musicName");
		String timeLength = request.getParameter("timeLength");
		String musicTypeId = request.getParameter("musicTypeId");
		String specialId = request.getParameter("specialId");
		String musicListId = request.getParameter("musicListId");
		String singerId = request.getParameter("singerId");
		
		music.setMusicName(musicName);
		music.setTimeLength(timeLength);
		music.setMusicTypeId(Integer.parseInt(musicTypeId));
		music.setSpecialId(Integer.parseInt(specialId));
		music.setMusicListId(Integer.parseInt(musicListId));
		music.setSingerId(Integer.parseInt(singerId));
		musicService.addMusic(music);
		return "/admin/music-add";
	}
	
	@RequestMapping("music_update_show")
	public String music_update_show(Integer musicId,Model model) {
		Map<String, Object> musicMap = musicService.queryMusicId(musicId);
		model.addAttribute("musicMap", musicMap);
		List<Map<String, Object>> musicTypeList = musicTypeService.queryMusicType();
		List<Map<String, Object>> specialList = specialService.querySpecial();
		List<Map<String, Object>> musicListList = musicListService.queryMusicList();
		List<Map<String, Object>> singerList = singerService.querySinger();
		model.addAttribute("musicTypeList", musicTypeList);
		model.addAttribute("specialList", specialList);
		model.addAttribute("musicListList", musicListList);
		model.addAttribute("singerList", singerList);
		return "/admin/music-edit";
	}
	
	@RequestMapping("music_update_submit")
	public String music_update_submit(Music music,Model model) {
		musicService.editMusic(music);
		return "/admin/music-edit";
	}
	
	@RequestMapping("music_delete")
	public String music_delete(Integer musicId) {
		musicService.delMusic(musicId);
		return "/admin/music";
	}
}
